Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip additional_paths from the asset paths generated in the file.js rule #403

Merged
merged 14 commits into from
Mar 26, 2024
Merged
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Changes since the last non-beta release.

_Please add entries here for your pull requests that are not yet released._

### Changed
- Just like with the `source_path` the paths in `additional_paths` will now also be stripped from asset paths. [PR 403](https://github.com/shakacode/shakapacker/pull/403) by [paypro-leon](https://github.com/paypro-leon).

## [v7.2.1] - December 30, 2023

### Fixed
Expand Down
2 changes: 2 additions & 0 deletions package/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@ if (config.manifest_path) {
config.manifestPath = resolve(config.outputPath, 'manifest.json')
}

config.includePaths = [config.source_path, ...config.additional_paths]

module.exports = config
1 change: 1 addition & 0 deletions package/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ declare module 'shakapacker' {
publicPath: string
publicPathWithoutCDN: string
manifestPath: string
includePaths: string[]
}

export interface Env {
Expand Down
2 changes: 1 addition & 1 deletion package/rules/__tests__/babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jest.mock("../../config", () => {
const original = jest.requireActual("../../config");
return {
...original,
additional_paths: [...original.additional_paths, "node_modules/included"],
includePaths: [...original.includePaths, "node_modules/included"]
};
});

Expand Down
2 changes: 1 addition & 1 deletion package/rules/__tests__/esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jest.mock("../../config", () => {
return {
...original,
webpack_loader: "esbuild",
additional_paths: [...original.additional_paths, "node_modules/included"],
includePaths: [...original.includePaths, "node_modules/included"]
};
});

Expand Down
17 changes: 17 additions & 0 deletions package/rules/__tests__/file.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
const file = require('../file')

jest.mock("../../config", () => {
const original = jest.requireActual("../../config");
return {
...original,
includePaths: [...original.includePaths, 'app/assets']
};
});

describe('file', () => {
test('test expected file types', () => {
const types = [
Expand Down Expand Up @@ -59,4 +67,13 @@ describe('file', () => {
'static/images/nested/deeply/[name]-[hash][ext][query]'
);
});

test('correct generated output path is returned for all include paths', () => {
const pathData = {
filename: 'app/assets/images/image.svg',
};
expect(file.generator.filename(pathData)).toEqual(
'static/images/[name]-[hash][ext][query]'
);
});
})
2 changes: 1 addition & 1 deletion package/rules/__tests__/swc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jest.mock("../../config", () => {
return {
...original,
webpack_loader: "swc",
additional_paths: [...original.additional_paths, "node_modules/included"],
includePaths: [...original.includePaths, "node_modules/included"],
};
});

Expand Down
9 changes: 6 additions & 3 deletions package/rules/file.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
const { dirname } = require('path')
const { source_path: sourcePath } = require('../config')
const { includePaths } = require('../config')

module.exports = {
test: /\.(bmp|gif|jpe?g|png|tiff|ico|avif|webp|eot|otf|ttf|woff|woff2|svg)$/,
exclude: /\.(js|mjs|jsx|ts|tsx)$/,
type: 'asset/resource',
generator: {
filename: (pathData) => {
const folders = dirname(pathData.filename)
.replace(`${sourcePath}`, '')
const path = dirname(pathData.filename)
const selectedIncludePath = includePaths.find((includePath) => path.includes(includePath))

const folders = path
.replace(`${selectedIncludePath}`, '')
.split('/')
.filter(Boolean)

Expand Down
7 changes: 2 additions & 5 deletions package/rules/jscommon.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
const { resolve } = require('path')
const { realpathSync } = require('fs')
const {
source_path: sourcePath,
additional_paths: additionalPaths
} = require('../config')
const { includePaths } = require('../config')

const inclusions = [sourcePath, ...additionalPaths].map(p => {
const inclusions = includePaths.map(p => {
try {
return realpathSync(p)
} catch (e) {
Expand Down
11 changes: 5 additions & 6 deletions package/rules/less.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
const path = require('path')
const { canProcess } = require('../utils/helpers')
const getStyleRule = require('../utils/get_style_rule')

const {
additional_paths: paths,
source_path: sourcePath
} = require('../config')
const { includePaths } = require('../config')

module.exports = canProcess('less-loader', (resolvedPath) =>
getStyleRule(/\.(less)(\.erb)?$/i, [
{
loader: resolvedPath,
options: {
lessOptions: {
paths: [path.resolve(__dirname, 'node_modules'), sourcePath, ...paths]
paths: [
path.resolve(__dirname, 'node_modules'),
...includePaths
]
},
sourceMap: true
}
Expand Down
9 changes: 2 additions & 7 deletions package/rules/stylus.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
const path = require('path')
const { canProcess } = require('../utils/helpers')
const getStyleRule = require('../utils/get_style_rule')

const {
additional_paths: paths,
source_path: sourcePath
} = require('../config')
const { includePaths } = require('../config')

module.exports = canProcess('stylus-loader', (resolvedPath) =>
getStyleRule(/\.(styl(us)?)(\.erb)?$/i, [
Expand All @@ -15,8 +11,7 @@ module.exports = canProcess('stylus-loader', (resolvedPath) =>
stylusOptions: {
include: [
path.resolve(__dirname, 'node_modules'),
sourcePath,
...paths
...includePaths
]
},
sourceMap: true
Expand Down