Skip to content

Commit

Permalink
fix: serializing big strings in sourceMap
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Dec 10, 2020
1 parent 4277d4d commit e786413
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
"babel-eslint": "^10.1.0",
"babel-jest": "^26.6.3",
"bootstrap": "^4.5.3",
"cross-env": "^7.0.2",
"css-loader": "^5.0.1",
"del": "^6.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ class MiniCssExtractPlugin {
new SourceMapSource(
content,
m.readableIdentifier(requestShortener),
m.sourceMap
m.sourceMap.toString()
)
);
} else {
Expand Down
5 changes: 4 additions & 1 deletion src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,10 @@ export function pitch(request) {
context: module.context,
content: Buffer.from(content),
media,
sourceMap,
sourceMap: sourceMap
? Buffer.from(JSON.stringify(sourceMap))
: // eslint-disable-next-line no-undefined
undefined,
};
});
}
Expand Down
21 changes: 21 additions & 0 deletions test/TestCases.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ describe('TestCases', () => {

clearDirectory(outputDirectory);

afterEach(() => {
jest.clearAllMocks();
});

for (const directory of tests) {
if (directory === 'auxiliaryAssets' && webpack.version[0] === '4') {
// eslint-disable-next-line no-continue
Expand All @@ -98,6 +102,23 @@ describe('TestCases', () => {
if (!/^(\.|_)/.test(directory)) {
// eslint-disable-next-line no-loop-func
it(`${directory} should compile to the expected result`, (done) => {
if (directory !== 'serializingBigStrings') {
const originalRegister = webpack.util.serialization.register;

webpack.util.serialization.register = jest
.fn()
.mockImplementation((...args) => {
if (args[1].startsWith('mini-css-extract-plugin')) {
// eslint-disable-next-line no-param-reassign
args[1] = args[1].replace(/dist/, 'src');

return originalRegister(...args);
}

return originalRegister(...args);
});
}

const directoryForCase = path.resolve(casesDirectory, directory);
const outputDirectoryForCase = path.resolve(outputDirectory, directory);
// eslint-disable-next-line import/no-dynamic-require, global-require
Expand Down
35 changes: 35 additions & 0 deletions test/cases/serializingBigStrings/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Self from '../../../src/index';

module.exports = {
cache: { type: 'filesystem' },
entry: 'bootstrap/dist/css/bootstrap.css',
module: {
rules: [
{
test: /\.css$/,
use: [
Self.loader,
{
loader: 'css-loader',
options: {
sourceMap: true,
},
},
],
},
],
},

plugins: [
new Self(),
{
apply(compiler) {
compiler.hooks.infrastructureLog.tap('test', (origin, type, args) => {
if (type === 'warn' || type === 'error') {
throw new Error(`<${type}> [${origin}] ${args.toString()}`);
}
});
},
},
],
};

0 comments on commit e786413

Please sign in to comment.