Skip to content

Commit

Permalink
fix: update loader for HMR
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Jul 17, 2020
1 parent f9785d0 commit fdf1ee8
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 58 deletions.
8 changes: 5 additions & 3 deletions core/loader/src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fs from 'fs';
import * as chalk from 'chalk';
import { getOptions } from 'loader-utils';
import { loader } from 'webpack';

import {
InstrumentOptions,
Expand All @@ -9,9 +10,10 @@ import {

import { addStoriesDoc, removeStoriesDoc } from './store';

module.exports.pitch = async function() {
const options: InstrumentOptions = getOptions(this) || {};
const filePath = this.resource;
module.exports = async function() {
const context = (this as unknown) as loader.LoaderContext;
const options: InstrumentOptions = getOptions(context) || {};
const filePath = context.resource;
const source = fs.readFileSync(filePath, 'utf8');
const { transformed, ...store } = await parseStories(
source,
Expand Down
17 changes: 0 additions & 17 deletions core/loader/src/replaceSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,12 @@ ${stories
}
`;
const exports = `module.exports = store;\n`;
const hmr = `
if (module.hot) {
module.hot.accept(function() {
console.log('HMR CALLBACK',this);
});
${stories
.map(
story => `
module.hot.accept(${story.relPath}, function() {
console.log('HMR',${story.relPath});
});
`,
)
.join('\n')}
}
`;
const newContent = `
${imports}
${storeConst}
store.config = ${configFilePath ? 'configJSON.default ||' : ''} configJSON;
store.buildConfig = ${config ? JSON.stringify(config) : '{}'};
${loadStories}
${hmr}
${exports}
`;
return newContent;
Expand Down
11 changes: 5 additions & 6 deletions core/loader/src/runtimeLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@ import { deepMergeArrays, defaultBuildConfig } from '@component-controls/core';

import { loadConfiguration, extractStories } from '@component-controls/config';
import { stringifyRequest } from 'loader-utils';
import { loader } from 'webpack';
import { replaceSource, StoryPath } from './replaceSource';
import { store, reserveStories } from './store';

module.exports = function(content: string) {
//@ts-ignore
const params = JSON.parse(this.query.slice(1));
//@ts-ignore
const config = loadConfiguration(this.rootContext, params.config);
const context = (this as unknown) as loader.LoaderContext;
const params = JSON.parse(context.query.slice(1));
const config = loadConfiguration(context.rootContext, params.config);
store.buildConfig = config?.config
? deepMergeArrays(defaultBuildConfig, config.config)
: defaultBuildConfig;

const stories: StoryPath[] = (config ? extractStories(config) || [] : []).map(
fileName => ({
absPath: fileName,
//@ts-ignore
relPath: stringifyRequest(this, fileName),
relPath: stringifyRequest(context, fileName),
}),
);
reserveStories(stories.map(story => story.absPath));
Expand Down
19 changes: 5 additions & 14 deletions core/webpack-compile/src/utilities.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import webpack, {
Configuration,
Compiler,
Stats,
HotModuleReplacementPlugin,
} from 'webpack';
import webpack, { Configuration, Compiler, Stats } from 'webpack';
import * as path from 'path';
const chalk = require('chalk');
import LoaderPlugin from '@component-controls/loader/plugin';
Expand Down Expand Up @@ -31,8 +26,7 @@ const createConfig = (options: CompileRunProps): webpack.Configuration => {
config: configPath,
escapeOutput: mode === 'development',
}),
new HotModuleReplacementPlugin({}),
];
].filter(Boolean);
const webpackConfig = mergeWebpackConfig(
{
mode,
Expand Down Expand Up @@ -100,16 +94,13 @@ export const runCompiler = (
console.error(error);
return reject(error);
}

const { store } = require('@component-controls/loader/store');
console.log(
chalk.bgRgb(244, 147, 66)('@bundle generated: '),
require.resolve('@component-controls/loader/store'),
chalk.bgRgb(244, 147, 66)(`${store.stores.length} documents compiled`),
);
const { store } = require('@component-controls/loader/store');
console.log(
chalk.bgRgb(244, 147, 66)('@end compilation'),
`${store.stores.length} documents compiled`,
);

resolve({ store, stats });
});
});
Expand Down
1 change: 1 addition & 0 deletions core/webpack-configs/src/instrument/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const instrument: Configuration = {
test: /\.(md|mdx)$/,
exclude: [/node_modules/],
loader: '@component-controls/loader/loader',
enforce: 'pre',
options: {
mdx: {
transformMDX: true,
Expand Down
1 change: 1 addition & 0 deletions core/webpack-configs/src/react-docgen-typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const reactDocgenTypescript: Configuration = {
test: /\.(md|mdx)$/,
exclude: [/node_modules/],
loader: '@component-controls/loader/loader',
enforce: 'pre',
options: {
propsLoaders: [
{
Expand Down
1 change: 1 addition & 0 deletions core/webpack-configs/src/react-docgen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const reactDocgen: Configuration = {
test: /\.(md|mdx)$/,
exclude: [/node_modules/],
loader: '@component-controls/loader/loader',
enforce: 'pre',
options: {
propsLoaders: [
{
Expand Down
40 changes: 22 additions & 18 deletions core/webpack-configs/src/react/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,6 @@ export const react: PresetType = (options?: PresetOptions) => {
},
],
},
{
test: /\.(eot|md|svg|ico|jpg|jpeg|png|gif|ttf|woff|woff2|pdf|mp4|web|wav|mp3|m4a|aac|oga)$/i,
exclude: [/node_modules/],
loader: 'url-loader',
options: {
limit: 25000,
name: '[name].[hash].[ext]',
publicPath: '/static',
outputPath: path.relative(
options?.distFolder || process.cwd(),
path.resolve(options?.outputFolder || process.cwd(), 'static'),
),
},
},
{
test: /\.(ts|tsx)$/,
exclude: [/node_modules/],
Expand All @@ -50,10 +36,6 @@ export const react: PresetType = (options?: PresetOptions) => {
},
],
},
{
test: /\.txt$/i,
use: require.resolve('raw-loader'),
},
{
test: /\.(md|mdx)$/i,
exclude: [/node_modules/],
Expand All @@ -65,10 +47,31 @@ export const react: PresetType = (options?: PresetOptions) => {
],
},
},
{
test: /\.(eot|md|svg|ico|jpg|jpeg|png|gif|ttf|woff|woff2|pdf|mp4|web|wav|mp3|m4a|aac|oga)$/i,
exclude: [/node_modules/],
loader: 'url-loader',
options: {
limit: 25000,
name: '[name].[hash].[ext]',
publicPath: '/static',
outputPath: path.relative(
options?.distFolder || process.cwd(),
path.resolve(options?.outputFolder || process.cwd(), 'static'),
),
},
},

{
test: /\.txt$/i,
use: require.resolve('raw-loader'),
},

{
test: /\.(md|mdx)$/i,
exclude: [/node_modules/],
loader: '@component-controls/loader/loader',
enforce: 'pre',
options: {
mdx: {
transformMDX: true,
Expand All @@ -79,6 +82,7 @@ export const react: PresetType = (options?: PresetOptions) => {
test: /\.(story|stories|doc|docs).(js|jsx|ts|tsx)$/,
loader: '@component-controls/loader/loader',
exclude: [/node_modules/],
enforce: 'pre',
},
],
},
Expand Down

0 comments on commit fdf1ee8

Please sign in to comment.