Skip to content

Commit

Permalink
Merge pull request #119 from django-webpack/fix/auto-publicPath
Browse files Browse the repository at this point in the history
Fix auto publicPath
  • Loading branch information
amandasavluchinske authored Dec 18, 2023
2 parents 6298d1b + 70cb95f commit 93715d4
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 12 deletions.
6 changes: 5 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,11 @@ class BundleTrackerPlugin {
}

if (this.options.publicPath) {
fileInfo.publicPath = this.options.publicPath + assetName;
if (this.options.publicPath === 'auto') {
fileInfo.publicPath = 'auto';
} else {
fileInfo.publicPath = this.options.publicPath + assetName;
}
}

if (this.options.relativePath === true) {
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
"pretty": "prettier --loglevel warn --write lib/*.js tests/*.js",
"pretty-lint": "prettier --check lib/*.js tests/*.js",
"pretest": "npm run pretty-lint",
"test": "jest --runInBand --env node",
"test-debug": "node --inspect-brk=0.0.0.0 node_modules/jest/bin/jest --runInBand --env node",
"test": "NODE_OPTIONS=--openssl-legacy-provider jest --runInBand --env node",
"test-debug": "NODE_OPTIONS=--openssl-legacy-provider node --inspect-brk=0.0.0.0 node_modules/jest/bin/jest --runInBand --env node",
"posttest": "tsc",
"test-watch": "jest --runInBand --env node --watchAll",
"ci": "npm run pretest && jest --runInBand --coverage --env node && npm run posttest"
"ci": "NODE_OPTIONS=--openssl-legacy-provider npm run pretest && jest --runInBand --coverage --env node && npm run posttest"
},
"jest": {
"setupFilesAfterEnv": [
Expand Down Expand Up @@ -59,17 +59,17 @@
"cz-conventional-changelog": "3.3.0",
"eslint": "^6.8.0",
"file-loader": "^5.1.0",
"jest": "^25.5.4",
"jest": "^29.7.0",
"jest-extended": "^0.11.5",
"mini-css-extract-plugin": "^1.6.2",
"prettier": "^1.19.1",
"standard-version": "^9.5.0",
"style-loader": "^1.3.0",
"tslint": "^6.1.0",
"typescript": "^3.9.10",
"webpack": "^4.46.0",
"typescript": "^5.3.2",
"webpack": "^4.47.0",
"webpack-cli": "^4.10.0",
"webpack5": "npm:webpack@^5.83.1"
"webpack5": "npm:webpack@^5.89.0"
},
"config": {
"commitizen": {
Expand Down
44 changes: 43 additions & 1 deletion tests/base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const CompressionPlugin = require('compression-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const BundleTrackerPlugin = require('../lib/index.js');

jest.setTimeout(120000);
jest.setTimeout(30000);
process.on('unhandledRejection', r => console.log(r));
process.traceDeprecation = true;

Expand Down Expand Up @@ -773,4 +773,46 @@ describe('BundleTrackerPlugin bases tests', () => {
expectWarnings,
);
});

it('It should support publicPath: "auto"', done => {
const expectErrors = null;
const expectWarnings = getWebpack4WarningMessage();

testPlugin(
webpack,
{
context: __dirname,
entry: path.resolve(__dirname, 'fixtures', 'index.js'),
output: {
path: OUTPUT_DIR,
filename: 'js/[name].js',
publicPath: 'auto',
},
plugins: [
new BundleTrackerPlugin({
path: OUTPUT_DIR,
filename: 'webpack-stats.json',
}),
],
},
{
status: 'done',
publicPath: 'auto',
chunks: {
main: ['js/main.js'],
},
assets: {
'js/main.js': {
name: 'js/main.js',
path: OUTPUT_DIR + '/js/main.js',
publicPath: 'auto',
},
},
},
'webpack-stats.json',
done,
expectErrors,
expectWarnings,
);
});
});
4 changes: 2 additions & 2 deletions tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const OUTPUT_DIR = fs.mkdtempSync(path.join(os.tmpdir(), 'wbt-tests-'));

function testPlugin(webpack, webpackConfig, expectedResults, outputFile, done, expectErrors, expectWarnings) {
webpack(webpackConfig, (err, stats) => {
const compilationErrors = (stats.compilation.errors || []).join('\n');
const compilationWarnings = (stats.compilation.warnings || []).join('\n');
const compilationErrors = (stats?.compilation?.errors || []).join('\n');
const compilationWarnings = (stats?.compilation?.warnings || []).join('\n');

expect(err).toBeFalsy();

Expand Down
44 changes: 43 additions & 1 deletion tests/webpack5.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const CompressionPlugin = require('compression-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const BundleTrackerPlugin = require('../lib/index.js');

jest.setTimeout(120000);
jest.setTimeout(30000);
process.on('unhandledRejection', r => console.log(r));
process.traceDeprecation = true;

Expand Down Expand Up @@ -772,4 +772,46 @@ describe('BundleTrackerPlugin bases tests', () => {
expectWarnings,
);
});

it('It should support publicPath: "auto"', done => {
const expectErrors = null;
const expectWarnings = getWebpack5WarningMessage();

testPlugin(
webpack5,
{
context: __dirname,
entry: path.resolve(__dirname, 'fixtures', 'index.js'),
output: {
path: OUTPUT_DIR,
filename: 'js/[name].js',
publicPath: 'auto',
},
plugins: [
new BundleTrackerPlugin({
path: OUTPUT_DIR,
filename: 'webpack-stats.json',
}),
],
},
{
status: 'done',
publicPath: 'auto',
chunks: {
main: ['js/main.js'],
},
assets: {
'js/main.js': {
name: 'js/main.js',
path: OUTPUT_DIR + '/js/main.js',
publicPath: 'auto',
},
},
},
'webpack-stats.json',
done,
expectErrors,
expectWarnings,
);
});
});
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"checkJs": true, /* Report errors in .js files. */
"noEmit": true, /* Do not emit outputs. */
"lib": ["es2017"],
"skipLibCheck": true,

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
Expand Down

0 comments on commit 93715d4

Please sign in to comment.